import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from "../views/Home.vue"
import Welcome from "../views/Welcome.vue"
import Users from '../components/user/Users.vue'
import Adduser from '../components/user/Adduser.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Categories from '../components/goods/Categories.vue'
import Params from '../components/goods/Params.vue'
import Goods from '../components/goods/Goods.vue'
import Addgoods from '../components/goods/Addgoods.vue'
import Editgood from '../components/goods/Editgood.vue'
import Orders from '../components/order/Orders.vue'
import Reports from '../components/report/Reports.vue'
Vue.use(VueRouter)
const routes = [
  {path:'/',redirect: '/Login'},
  {path:'/Login',component:Login},
  {path:'/Home',component:Home,
   children:[
     {path:'/Home',redirect: '/Welcome'} ,
     {path:'/Welcome',component:Welcome},
     {path:'/Users',component:Users},
     {path:'/Adduser',component:Adduser},
     {path:'/Rights',component:Rights},
     {path:'/Roles',component:Roles},
     {path:'/Categories',component:Categories},
     {path:'/Params',component:Params},
     {path:'/Goods',component:Goods},
     {path:'/Addgoods',component:Addgoods},
     {
        
         path:'/Editgood',
         component:Editgood,
         props($route){
              return {good:$route.query.good}
         }
     },
     {path:'/Orders',component:Orders},
     {path:'/Reports',component:Reports}
     ]
  },
]
const router = new VueRouter({
  routes
})
//路由前置守卫
router.beforeEach((to, from, next) => {
  // to 到那个组件
  // form 从那个组件来
  // next() 放行 或 next('/路由名')强制跳转
   if(to.path==='/Login') return next()
  const token= window.sessionStorage.getItem('token')
  if(!token) return next('/Login')
  next()
  
})
export default router
